package com.yili.oa.service.impl;

import com.yili.oa.dao.AdminMapper;
import com.yili.oa.dao.extend.AdminExtendMapper;
import com.yili.oa.entity.SearchBase;
import com.yili.oa.models.Admin;
import com.yili.oa.models.AdminExample;
import com.yili.oa.models.ExtractsRecord;
import com.yili.oa.service.AdminService;
import com.yili.oa.utils.Exception.AjaxException;
import com.yili.oa.utils.ListUtils;
import com.yili.oa.utils.annotate.DataBase;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lxr
 * @version v0.01
 * @date 2019/7/15 0015
 * @email 200890949@qq.com
 */
@SuppressWarnings("ALL")
@Service
@Transactional
@DataBase("db_manage")
public class AdminSvImpl implements AdminService {

    @Autowired
    private AdminMapper mapper;

    @Autowired
    private AdminExtendMapper adminExtendMapper;

    @Override
    public Admin getByAccount(String account) {
        AdminExample example = new AdminExample();
        AdminExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(account);
        List<Admin> Admins = mapper.selectByExample(example);
        Admin admin =new Admin();
        if (ListUtils.isNotNullOrEmpty(Admins)){
            admin = Admins.get(0);
        }
        return admin;
    }

    @Override
    public List<Admin> getList(SearchBase searchBase,String userName) {
        AdminExample example = new AdminExample();
        AdminExample.Criteria criteria = example.createCriteria();
        if (userName!=null){
            criteria.andParentEqualTo(userName);
        }
        if (StringUtils.isNotBlank(searchBase.getId())){
            criteria.andUsernameEqualTo(searchBase.getId());
        }
        //排除管理员
        criteria.andIsAdminEqualTo(0);
        return mapper.selectByExample(example);
    }

    @Override
    public void update(Integer id, Integer status) {
        Admin admin = mapper.selectByPrimaryKey(id);
        if (admin==null){
            throw new AjaxException("该代理不存在");
        }
        admin.setState(status);
        mapper.updateByPrimaryKeySelective(admin);
    }

    @Override
    public void delSignIn(List<String> list) {

    }

    @Override
    public List<Admin> getUnderChildren(String username) {
        AdminExample example = new AdminExample();
        AdminExample.Criteria criteria = example.createCriteria();
        criteria.andParentEqualTo(username);
        List<Admin> adminList = mapper.selectByExample(example);
        return adminList;
    }

/*    @Override
    public void update(Admin admin){
        String availableCommission = admin.getAvailableCommission();
        List<LowerData> list = JSONObject.parseArray(availableCommission, LowerData.class);
        list.forEach(lowerData->{
            BigDecimal bigDecimal = BigDecimal.ZERO;
            lowerData.setAvailableCommission(bigDecimal);
        });
        Object o = JSONObject.toJSON(list);
        admin.setAvailableCommission(o.toString());
        int i = adminExtendMapper.updateByIdAndVersion(admin);
        if(i!=1){
            throw new AjaxException("可提取金额已经发生改变！");
        }
    }*/

    @Override
    public void reset(Integer dealerId) {
        Admin admin = mapper.selectByPrimaryKey(dealerId);
        if (admin==null){
            throw new AjaxException("该代理不存在");
        }
        Object obj = new SimpleHash("MD5", "888888", null, 5);
        admin.setPassword(obj.toString());
        mapper.updateByPrimaryKeySelective(admin);
    }

    @Override
    public void update(Admin admin) {

    }

    @Override
    public int refund(ExtractsRecord extractsRecord) {
        return 0;
    }

/*    @Override
    public int refund(ExtractsRecord extractsRecord) {
        //获取最新的用户信息
        Admin admin = getByAccount(extractsRecord.getUserId()+"");
        String str = extractsRecord.getDetail();
        List<LowerData> lds = JSONObject.parseArray(str, LowerData.class);

        //提交的取款明细
        String detail = extractsRecord.getDetail();
        List<LowerData> list = JSONObject.parseArray(detail, LowerData.class);
        for (int i = 0; i <list.size() ; i++) {
            LowerData ld = list.get(i);
            LowerData temp = lds.get(i);
            temp.setAvailableCommission(temp.getAvailableCommission().add(ld.getAvailableCommission()));
        }
        Object o = JSONObject.toJSON(lds);
        admin.setAvailableCommission(o.toString());
        return adminExtendMapper.updateByIdAndVersion(admin);
    }*/
}
